package com.zx._12_算法.剑指offer._2022;

import com.zx._12_算法.TreeNode;

/**
 * https://leetcode.cn/problems/ping-heng-er-cha-shu-lcof/
 */
public class 平衡二叉树 {

    boolean res = true;

    public boolean isBalanced(TreeNode root) {
        depth(root);
        return res;
    }

    private int depth(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int left = depth(root.left);
        int right = depth(root.right);

        if (Math.abs(left - right) > 1) {
            res = false;
        }

        return left > right ? ++left : ++right;
    }
}
